import {GetQtController, PubSubManager} from "../QtUtils";

export const COMPONENT_TYPE_MENU = "menu"   //菜单性质的，类似于路由（唯一）
export const COMPONENT_TYPE_VIEW = "view"   //只是页面
export const DIRECTION_HIDE_LEFT = 2<<1   //左边隐藏
export const DIRECTION_HIDE_RIGHT = 2<<2   //右边隐藏
export const DIRECTION_HIDE_TOP = 2<<3   //上边隐藏
export const DIRECTION_HIDE_BOTTOM = 2<<4   //下边隐藏
export const DIRECTION_HIDE_ALL = DIRECTION_HIDE_LEFT|DIRECTION_HIDE_RIGHT|DIRECTION_HIDE_TOP|DIRECTION_HIDE_BOTTOM //隐藏所有
export const DIRECTION_HIDE_NONE = 0 //都不隐藏


export class MutexParam {
  constructor() {
    this.groupName = '';
    this.list = [];
  }
}

export class FrameManager {
  constructor() {
    this._instance = null;
    this._lastComponentName = '';
    this._curComponentName = '';
    this._mapViewShow = {};   //是否显示的 {"jinggang_detail":{"isShow":true,"z_index":999}}
    this._mutexComponentList = [];    //互斥组MutexParam的数组，[{"groupName":"aa","list":["Editproperty","Point"]}]
    let mutexParam = new MutexParam();
    mutexParam.groupName = 'menu';
    this.addMutexComponent(mutexParam);
    this._nCurDirection = DIRECTION_HIDE_NONE;
  }

  static getInstance() {
    if(!this._instance) {
      this._instance = new FrameManager();
    }
    return this._instance;
  }

  getLastComponentName() {
    return this._lastComponentName;
  }

  getCurComponentName() {
    return this._curComponentName;
  }

  addMenu(strName) {
    this.addMutexComponentSingle('menu',strName);
  }

  addComponentName(strName) {
    this.GetViewParam(strName);
    this.setViewShow(strName,false,false);
  }

  isContainerComponentName(strName) {
    let mutexParam = this.getMutexParam('menu');
    return mutexParam.list.includes(strName);
  }

  /**
   * 添加互斥组成员
   * @param strGroupName
   * @param strComponentName
   */
  addMutexComponentSingle(strGroupName,strComponentName) {
    let group = null;
    for(let index in this._mutexComponentList) {
      if(this._mutexComponentList[index].groupName == strGroupName) {
        group = this._mutexComponentList[index];
        break;
      }
    }
    if(!group) {
      group = {};
      group.groupName = strGroupName;
      group.list = [];
      this._mutexComponentList.push(group);
    }
    if(!group.list.includes(strComponentName)) {
      group.list.push(strComponentName);
    }
  }

  /**
   * 添加互斥组
   * @param mutexParam
   */
  addMutexComponent(mutexParam) {
    let group = null;
    for(let index in this._mutexComponentList) {
      if(this._mutexComponentList[index].groupName == mutexParam.groupName) {
        this._mutexComponentList[index] = mutexParam;
        group = this._mutexComponentList[index];
        break;
      }
    }
    if(!group) {
      this._mutexComponentList.push(mutexParam);
    }
  }

  getMutexParam(strGroup) {
    for(let index in this._mutexComponentList) {
      if(this._mutexComponentList[index].groupName == strGroup) {
        return this._mutexComponentList[index];
      }
    }
    let mutexParam = new MutexParam();
    mutexParam.groupName = strGroup;
    this.addMutexComponent(mutexParam);

    return mutexParam;
  }

  /**
   * 是否可见
   * @param strName
   * @returns {*}
   */
  isViewShow(strName) {
    if(!this._mapViewShow.hasOwnProperty(strName)) {
      this.setViewShow(strName,false,false)
    }
    return this._mapViewShow[strName].isShow;
  }

  /**
   * 获取View的Param
   * @param strName
   * @returns {*}
   */
  GetViewParam(strName) {
    if(!this._mapViewShow.hasOwnProperty(strName)) {
      let param = {};
      param.isShow = false;
      param.isPreShow = false;
      param.z_index = 1;
      this._mapViewShow[strName] = param;
    }
    return this._mapViewShow[strName];
  }

  /**
   * 隐藏所有的页面
   */
  setAllViewHide() {
    for(let key in this._mapViewShow) {
      this.setViewShow(key,false);
    }
  }

  /**
   * 隐藏互斥组
   * @param groupName
   */
  hideMutexViews(groupName) {
    for(let index in this._mutexComponentList) {
      if(this._mutexComponentList[index].groupName == groupName) {
        let viewList = this._mutexComponentList[index].list;
        for(let j in viewList) {
          this.setViewShow(viewList[j],false);
        }
      }
    }
  }

  /**
   * 设置是否可见
   * @param strName
   * @returns {*}
   */
  setViewShow(strName,isShow,isSendPub = true) {
    if(this.GetViewParam(strName).isShow == isShow) {
      return;
    }
    let hideList = [];
    let showList = [];
    //判断互斥
    if(isShow) {
      showList.push(strName);
      for(let index in this._mutexComponentList) {
        let mutexParam = this._mutexComponentList[index];
        if(mutexParam.list.includes(strName)) {
          //其他的则要隐藏
          for(let i in mutexParam.list) {
            if(mutexParam.list[i] != strName) {
              hideList.push(mutexParam.list[i]);
            }
          }
        }
      }
    }else {
      hideList.push(strName);
    }

    //onPre的方法
    let mergeList = showList.concat(hideList);
    if(isSendPub) {
      for(let index in mergeList) {
        let strName = mergeList[index];
        let viewParam = this.GetViewParam(strName);
        viewParam.isPreShow = viewParam.isShow;

        let preData = [];
        let isShow = showList.includes(strName);
        preData.push(strName);
        preData.push(isShow);
        PubSubManager.sendMessage('preViewShow',preData,[],false);
      }
    }
    //当前
    if(isSendPub) {
      //发送消息
      let zIndex = -1;
      for(let index in mergeList) {
        let strName = mergeList[index];
        let isShow = showList.includes(strName);

        let resultData = [];
        resultData.push(strName);
        resultData.push(isShow);
        zIndex = this._getZIndex(strName,isShow);
        resultData.push(zIndex);
        PubSubManager.sendMessage('setViewShow',resultData,[],false);
        PubSubManager.sendMessage('postViewShow',resultData,[],false);
      }
    }
  }

  /**
   * 获取zIndex
   * @param strName
   */
  _getZIndex(strName,isShow) {
    let param = this.GetViewParam(strName);
    //当前的
    let nIndex = -1;
    if(isShow) {
      //判断当前的index
      nIndex = 1;  //显示的话默认为1
      let maxIndexParam = null;
      let strMaxName = '';
      for(let key in this._mapViewShow) {
        if(!maxIndexParam) {
          maxIndexParam = this._mapViewShow[key];
        }
        let pTemp = this._mapViewShow[key];
        if(pTemp.isShow && pTemp.z_index > maxIndexParam.z_index) {
          maxIndexParam = param;
          strMaxName = key;
        }
      }
      if(maxIndexParam) {
        if(strMaxName == strName) {
          //是自己则index不做处理
          nIndex = maxIndexParam.z_index;
        }else {
          //不是自己则+1
          nIndex = maxIndexParam.z_index + 1;
        }
      }
    }
    param['isShow'] = isShow;
    param["z_index"] = nIndex;
    this._mapViewShow[strName] = param;
  }

  /**
   * 切换Component
   */
  switchComponent(strName) {
    if(strName == this._curComponentName) {
      return;
    }
    if(!this.getMutexParam('menu').list.includes(strName)) {
      return;
    }
    this._lastComponentName = this._curComponentName;
    this._curComponentName = strName;
    let data = {};
    data['curName'] = this._curComponentName;
    data['lastName'] = this._lastComponentName;
    this.setViewShow(strName,true);
    PubSubManager.sendMessage('switchComponent',data,[],false);
    GetQtController().Web_ChangeMenu(this._curComponentName,this._lastComponentName);
  }

  /**
   * 发送PubSub消息
   */
  sendPubSubMessage(strCode,data = null,excludeComponent = []) {
    PubSub.sendPubSubMessage(strCode,data,excludeComponent);
  }

  /**
   * 设置组件上下左右的隐藏情况
   * @param nDirectCode
   */
  setViewComponentDirectHide(nDirectCode) {
    this._nCurDirection = nDirectCode;
    let sData = [];
    sData.push(nDirectCode);
    PubSubManager.sendMessage('setViewComponentDirectHide',sData,[],false);
  }

  getViewComponentDirectHide() {
    return this._nCurDirection;
  }

  /**
   * 当前页面重新一次onEnter
   */
  restartEnter() {
    let list = [];
    for(let key in this._mapViewShow) {
      if(this._mapViewShow[key].isShow) {
        list.push(key);
      }
    }
    PubSubManager.sendMessage('restartEnter',list,[],false);
  }
}
